package mensa;

import java.util.List;
import java.util.Random;

public class Student extends Thread {
	private List<Kasse> kassen;
	private int id;
	
	public Student(List<Kasse> kassen, int id) {
		this.kassen = kassen;
		this.id = id;
	}
	
	public int id() {
		return id;
	}
	
	@Override
	public void run() {
		while (true) {
                    Kasse target = kassen.get(0);
                    for(Kasse k : kassen)
                            target = (target.queueLength() < k.queueLength()) ? target : k;

                    try {
                            System.out.println("Stundent " + id  + " stellt sich an Kasse " + target.getId() + " an");
                            target.addStudent(this);
                    } catch (InterruptedException e) {
                            return;
                    }
                    Random r = new Random();
                    int time = r.nextInt(4000) + 1000;
                    //Essen
                    try {
                            this.sleep(time);
                    } catch (InterruptedException e) {
                            return;
                    }
                    System.out.println("\t\t\t\t\t\t\t\t\tStundent " + id  + " hat gegessen");
                    time = r.nextInt(8000) + 1000;
                    //Wieder anstellen nach...
                    try {
                            this.sleep(time);
                    } catch (InterruptedException e) {
                            return;
                    }			
		}
	}
}
